package com.every.utils;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description:
 * @author: yh
 * @date: 2022/11/13
 */
@Slf4j
public class BirthdayUtil {
    /**
     * 计算两个时间相差天数
     *
     * @param startDate 开始时间  yyyyMMdd
     * @param endDate   结束时间  yyyyMMdd
     * @return 相差多少天
     * @author: yh
     * @date: 2022/11/13
     */
    public static int dayDiffer(String startDate, String endDate) {
        try {
            startDate = startDate.replaceAll("-", "");
            endDate = endDate.replaceAll("-", "");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(startDate));
            long start = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse(endDate));
            long end = calendar.getTimeInMillis();
            long betWeendays = (end - start) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(betWeendays));
        } catch (Exception e) {
            log.error("计算时间差异常：" + e.getMessage());
        }
        return -1;
    }

    /**
     * 构建字符串，小于10前边加0
     *
     * @param i 数字
     * @author: yh
     * @date: 2022/11/13
     */
    public static String buildNumber(int i) {
        if (i < 10 && i > 0) {
            return "0" + i;
        }
        return String.valueOf(i);
    }

    /**
     * 计算下个生日还剩多少天
     *
     * @param birthday 生日  阴历10|22 格式
     * @return 距离生日还有多少天
     * @author: yh
     * @date: 2022/11/13
     */
    public static int birthdayDiffer(String birthday) {
        Date date = DateUtil.date();
        //获得年的部分
        int year = DateUtil.year(date);
        //通过农历构建日期
        ChineseDate chineseDate = new ChineseDate(year, Integer.parseInt(birthday.substring(0, 2)), Integer.parseInt(birthday.substring(2,4)));

        String endDate = chineseDate.getGregorianYear() + buildNumber(chineseDate.getGregorianMonthBase1()) + buildNumber(chineseDate.getGregorianDay());
        String startDate = DateUtil.today();
        int i = dayDiffer(startDate, endDate);
        if(i < 0){
            // 本年生日已过
            // 计算下一年的阴历生日的公历时间
            chineseDate = new ChineseDate(year + 1, Integer.parseInt(birthday.substring(0, 2)), Integer.parseInt(birthday.substring(2,4)));
             endDate = (chineseDate.getGregorianYear()) + buildNumber(chineseDate.getGregorianMonthBase1()) + buildNumber(chineseDate.getGregorianDay());
           i = dayDiffer(startDate, endDate);
        }
        return i;
    }

    /**
     * 计算相遇多少天
     * @param startDate 阳历 20221020 格式
     * @author: yh
     * @date: 2022/11/17
     */
    public static int meetDiffer(String startDate){
        // 当前时间
        String currDate = DateUtil.today();

        // 当天算一天，所以 +1
        return dayDiffer(startDate, currDate) + 1;
    }

    public static void main(String[] args) {
        int i = birthdayDiffer("1030");
//        int i = dayDiffer("20221020", "20221117") + 1;
        System.out.println(i);
    }

}
